package com.nuc.wxh.vaccine_appointment.vaccines.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuc.wxh.vaccine_appointment.base.exception.VaccineException;
import com.nuc.wxh.vaccine_appointment.base.result.R;
import com.nuc.wxh.vaccine_appointment.base.result.ResultCodeEnum;
import com.nuc.wxh.vaccine_appointment.base.util.JwtInfo;
import com.nuc.wxh.vaccine_appointment.base.util.JwtUtils;
import com.nuc.wxh.vaccine_appointment.base.util.MD5;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.SysUser;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.LoginVo;
import com.nuc.wxh.vaccine_appointment.vaccines.service.AdminDoctorService;
import com.nuc.wxh.vaccine_appointment.vaccines.service.AdminHospitalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class login {

    @Autowired
    private AdminHospitalService adminHospitalService;
    @Autowired
    private AdminDoctorService adminDoctorService;

    @ApiOperation(value = "用户登录")
    @PostMapping("login")
    public R login(@ApiParam(value = "用户对象", required = true) @RequestBody LoginVo loginVo) {
        try {
            System.out.println(loginVo);
            System.out.println(loginVo.getRoles());
            if (loginVo.getRoles().equals("医生")) {
               String token = adminDoctorService.login(loginVo);
                return R.ok().data("token", token);
            } else if (loginVo.getRoles().equals("医院管理员")) {
               String token = adminHospitalService.login(loginVo);
                return R.ok().data("token", token);
            } else if (loginVo.getRoles().equals("管理员")) {
                System.out.println("11111111111111111");
                if (loginVo.getUsername().equals("admin") && loginVo.getPassword().equals("admin")) {

                    JwtInfo jwtInfo = new JwtInfo();
                    jwtInfo.setId("1234567890123456789");
                    jwtInfo.setRoles(loginVo.getRoles());
                    String token = JwtUtils.getJwtToken(jwtInfo, 12 * 60 * 60);
                    return R.ok().data("token", token);
                }

            }
        } catch (Exception e) {
            return R.error().message("登录失败");
        }
        return R.error().message("登录失败");
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("get-login-info")
    public R getLoginInfo(HttpServletRequest request) {
        try {
            JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
            return R.ok().data("userInfo", jwtInfo).data("roles", Arrays.asList(jwtInfo.getRoles()));
        } catch (Exception e) {
            throw new VaccineException(ResultCodeEnum.FETCH_USERINFO_ERROR);
        }
    }


    @ApiOperation("退出登录")
    @PostMapping("logout")
    public R logout() {
        return R.ok();
    }

}
